from 数据库操作.CreateMysqlCon import CreateMysqlCon

class InsertBlog:
    def __init__(self):
        self.con = CreateMysqlCon().get_con()
        if not self.con:
            raise ConnectionError("数据库连接失败，请检查配置或网络")
        self.cursor = self.con.cursor()

    def insert_blog(self, title, content, username):
        try:
            self.cursor.execute("SELECT ID FROM user_data WHERE username = %s", (username,))
            data = self.cursor.fetchone()
            user_id = None
            for ID in data:
                user_id = int(ID)
            sql = "INSERT INTO blog_list (title, details, user_id) VALUES (%s, %s, %s)"
            self.cursor.execute(sql, (title, content, user_id))
            self.con.commit()
            return True
        except Exception as e:
            return False
